package bookStore.service.business;

import java.util.List;

import bookStore.dao.business.AuthorDao;
import bookStore.dao.business.AuthorDaoImpl;
import bookStore.dao.business.BookDao;
import bookStore.dao.business.BookDaoImpl;
import bookStore.dao.business.ReviewDao;
import bookStore.dao.business.ReviewDaoImpl;
import bookStore.po.Book;
import bookStore.service.ServiceTemplate;
import bookStore.vo.BookVo;
import bookStore.vo.BookWithAuthorName;
import bookStore.vo.BookWithPicture;

public class BookServiceImpl extends ServiceTemplate implements BookService{
	BookDao dao;
	AuthorDao authorDao;
	ReviewDao reviewDao;
	
	public BookServiceImpl() {
		this.dao = new BookDaoImpl(this.connection);
		authorDao = new AuthorDaoImpl(connection);
		reviewDao = new ReviewDaoImpl(connection);
	}

	@Override
	public List<Book> queryAll(int offset, int pageSize) {
		// TODO Auto-generated method stub
		return dao.queryAll(offset, pageSize);
	}

	@Override
	public void save(Book t) {
		dao.save(t);
		
	}

	@Override
	public Book get(int id) {
		// TODO Auto-generated method stub
		return dao.get(id);
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return dao.count();
	}

	@Override
	public void update(Book t) {
		// TODO Auto- method stub
		dao.update(t);
	}

	@Override
	public void delete(int id) {
		
		dao.delete(id);
	}

	@Override
	public BookVo getBookVoByBookId(int bookId) {
		BookVo bookVo = new BookVo();
		
		Book book = dao.get(bookId);
		bookVo.setId(book.getId());
		bookVo.setAuthorId(book.getAuthorId());
		bookVo.setIntroduction(book.getIntroduction());
		bookVo.setPicture(book.getPicture());
		bookVo.setTitle(book.getTitle());
		
		bookVo.setAuthorName(authorDao.getAuthorNameById(book.getAuthorId()));
		bookVo.setNewestOpinions(reviewDao.getOpinionByBookId(bookId));
		bookVo.setScore(reviewDao.getAvgScoreByBook(bookId));
		
		return bookVo;
	}

	@Override
	public List<BookWithPicture> queryBookWithPictureByIdDesc(int offset, int pageSize) {
		return dao.queryBookWithPictureByIdDesc(offset, pageSize);
	}

	@Override
	public List<BookWithPicture> queryBookWithPictureByAuthor(int authorId, int offset, int pageSize) {
		return dao.queryBookWithPictureByAuthor(authorId, offset, pageSize);
	}

	@Override
	public List<BookWithPicture> queryBookWithPictureByScore(int offset, int pageSize) {
		return dao.queryBookWithPictureByScore(offset, pageSize);
	}

	@Override
	public List<BookWithPicture> queryBookWithPictureByScoreAndAuthor(int authorId, int offset, int pageSize) {
		return dao.queryBookWithPictureByScoreAndAuthor(authorId, offset, pageSize);
	}

	@Override
	public List<BookWithPicture> queryBookWithPictureByAuthorOrTitle(String authorOrTitle, int offset,
			int pageSize) {
		return dao.queryBookWithPictureByAuthorOrTitle(authorOrTitle, offset, pageSize);
	}

	@Override
	public long CountBookWithPictureByAuthorOrTitle(String authorOrTitle) {
		return dao.CountBookWithPictureByAuthorOrTitle(authorOrTitle);
	}

	@Override
	public List<BookVo> queryAllBookVo(int offset, int pageSize) {
		return dao.queryAllBookVo(offset, pageSize);
	}

	@Override
	public BookWithAuthorName getBookWithAuthorName(int bookId1) {
		return dao.getBookWithAuthorName(bookId1);
	}

}
